package com.journey.org.ui.video.recommend

import android.annotation.SuppressLint
import android.app.Application
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableInt
import com.journey.org.BR
import com.journey.org.R
import com.journey.org.app.HttpManager
import com.journey.org.app.requestBaoBase
import com.journey.org.entity.HomeRecommendEntity
import com.mvvmhabit.org.base.BaseViewModel
import com.mvvmhabit.org.base.MultiItemViewModel
import com.mvvmhabit.org.binding.command.BindingAction
import com.mvvmhabit.org.binding.command.BindingCommand
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * @Date: 2020/11/19 16:46
 * @Author:小妖
 * @Description: 推荐视频
 */
class RecommendViewModel(application: Application) : BaseViewModel(application) {

    val requestStateField = ObservableInt()

    val onClickAgainLoadCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            page = 0
            loadHomeRecommendListData()
        }
    })

    var page = 0

    val onRefreshCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            page = 0
            loadHomeRecommendListData()
        }
    })

    val onLoadMoreCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            ++page
            loadHomeRecommendListData()
        }
    })

    val itemBinding =
        ItemBinding.of<MultiItemViewModel<RecommendViewModel>> { itemBinding, _, item ->
            when (item.getItemType().toString()) {
                "title" -> { // 标题
                    itemBinding.set(BR.viewModel, R.layout.recommend_video_title_item)
                }
                "daily" -> { // 每日
                    itemBinding.set(BR.viewModel, R.layout.recommend_video_daily_item)
                }
                "today" -> { // 今日
                    itemBinding.set(BR.viewModel, R.layout.recommend_video_today_item)
                }
                "banner" -> {
                    itemBinding.set(BR.viewModel, R.layout.recommend_video_banner_item)
                }
            }
        }

    val items = ObservableArrayList<MultiItemViewModel<RecommendViewModel>>()

    @SuppressLint("CheckResult")
    fun loadHomeRecommendListData() {
        if (page == 0) {
            showLoadDialog()
        }
        requestBaoBase<List<HomeRecommendEntity>> {
            onLoader {
                HttpManager.getInstance().loadHomeRecommendListData(page)!!
            }
            onSuccess {
                if (it.isNotEmpty()) {
                    it.forEach { entity ->
                        when (entity.type) {
                            "squareCardCollection" -> {
                                val squareTitleViewModel =
                                    RecommendTitleViewModel(
                                        this@RecommendViewModel,
                                        entity.data.header.title
                                    )
                                squareTitleViewModel.multiItemType("title")
                                items.add(squareTitleViewModel)
                                entity.data.itemList.forEach { itemEntity ->
                                    val squareItemViewModel =
                                        RecommendDailyViewModel(
                                            this@RecommendViewModel,
                                            itemEntity
                                        )
                                    squareItemViewModel.multiItemType("daily")
                                    items.add(squareItemViewModel)
                                }
                            }
                            "banner2" -> {
                                val bannerViewModel =
                                    RecommendBannerViewModel(
                                        this@RecommendViewModel,
                                        entity.data.image
                                    )
                                bannerViewModel.multiItemType("banner")
                                items.add(bannerViewModel)
                            }
                            "textCard" -> {
                                val textCardTitleViewModel =
                                    RecommendTitleViewModel(
                                        this@RecommendViewModel,
                                        entity.data.text
                                    )
                                textCardTitleViewModel.multiItemType("title")
                                items.add(textCardTitleViewModel)
                            }
                            "followCard", "videoSmallCard" -> {
                                val followViewModel =
                                    RecommendTodayViewModel(
                                        this@RecommendViewModel,
                                        entity.data
                                    )
                                followViewModel.multiItemType("today")
                                items.add(followViewModel)
                            }
                        }
                    }
                }
            }
            onComplete {
                dismissLoadDialog()
            }
        }
    }
}